package com.zyk.filter;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
@Component
public class LoginFilter extends ZuulFilter {
	Logger log = LoggerFactory.getLogger(LoginFilter.class);
	//返回值表示该过滤器是否执行
	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public Object run() throws ZuulException {
		RequestContext context = RequestContext.getCurrentContext();
		HttpServletRequest request = context.getRequest();
		String token = request.getParameter("token");
		if(token==null) {
			log.warn("系统需要先登录");
			context.setSendZuulResponse(false);//拒绝访问
			context.setResponseStatusCode(200);
			try {
				context.getResponse().getWriter().write("token is null");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		log.info("=======ok=======");
		return null;
	}
	//返回字符串代表过滤器的类型 pre(之前) route(路由时) post(之后，也就是在route和error之后执行) error 
	@Override
	public String filterType() {
		return "pre";
	}
	//过滤器的执行顺序 返回值越小 过滤器优先级越高
	@Override
	public int filterOrder() {
		return 0;
	}

}
